<html><!-- Created using the cpp_pretty_printer from the dlib C++ library.  See http://dlib.net for updates. --><head><title>dlib C++ Library - find_optimal_parameters.h</title></head><body bgcolor='white'><pre>
<font color='#009900'>// Copyright (C) 2016  Davis E. King (davis@dlib.net)
</font><font color='#009900'>// License: Boost Software License   See LICENSE.txt for the full license.
</font><font color='#0000FF'>#ifndef</font> DLIB_fIND_OPTIMAL_PARAMETERS_Hh_
<font color='#0000FF'>#define</font> DLIB_fIND_OPTIMAL_PARAMETERS_Hh_

<font color='#0000FF'>#include</font> "<a style='text-decoration:none' href='../matrix.h.html'>../matrix.h</a>"
<font color='#0000FF'>#include</font> "<a style='text-decoration:none' href='find_optimal_parameters_abstract.h.html'>find_optimal_parameters_abstract.h</a>"
<font color='#0000FF'>#include</font> "<a style='text-decoration:none' href='optimization_bobyqa.h.html'>optimization_bobyqa.h</a>"
<font color='#0000FF'>#include</font> "<a style='text-decoration:none' href='optimization_line_search.h.html'>optimization_line_search.h</a>"

<font color='#0000FF'>namespace</font> dlib
<b>{</b>

<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
    <font color='#0000FF'>template</font> <font color='#5555FF'>&lt;</font>
        <font color='#0000FF'>typename</font> funct
        <font color='#5555FF'>&gt;</font>
    <font color='#0000FF'><u>double</u></font> <b><a name='find_optimal_parameters'></a>find_optimal_parameters</b> <font face='Lucida Console'>(</font>
        <font color='#0000FF'><u>double</u></font> initial_search_radius,
        <font color='#0000FF'><u>double</u></font> eps,
        <font color='#0000FF'>const</font> <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>int</u></font> max_f_evals,
        matrix<font color='#5555FF'>&lt;</font><font color='#0000FF'><u>double</u></font>,<font color='#979000'>0</font>,<font color='#979000'>1</font><font color='#5555FF'>&gt;</font><font color='#5555FF'>&amp;</font> x,
        <font color='#0000FF'>const</font> matrix<font color='#5555FF'>&lt;</font><font color='#0000FF'><u>double</u></font>,<font color='#979000'>0</font>,<font color='#979000'>1</font><font color='#5555FF'>&gt;</font><font color='#5555FF'>&amp;</font> x_lower,
        <font color='#0000FF'>const</font> matrix<font color='#5555FF'>&lt;</font><font color='#0000FF'><u>double</u></font>,<font color='#979000'>0</font>,<font color='#979000'>1</font><font color='#5555FF'>&gt;</font><font color='#5555FF'>&amp;</font> x_upper,
        <font color='#0000FF'>const</font> funct<font color='#5555FF'>&amp;</font> f
    <font face='Lucida Console'>)</font> 
    <b>{</b>
        <font color='#BB00BB'>DLIB_CASSERT</font><font face='Lucida Console'>(</font>x.<font color='#BB00BB'>size</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> <font color='#5555FF'>=</font><font color='#5555FF'>=</font> x_lower.<font color='#BB00BB'>size</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> <font color='#5555FF'>&amp;</font><font color='#5555FF'>&amp;</font> x_lower.<font color='#BB00BB'>size</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> <font color='#5555FF'>=</font><font color='#5555FF'>=</font> x_upper.<font color='#BB00BB'>size</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> <font color='#5555FF'>&amp;</font><font color='#5555FF'>&amp;</font> x.<font color='#BB00BB'>size</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> <font color='#5555FF'>&gt;</font> <font color='#979000'>0</font>,
            "<font color='#CC0000'>\t double find_optimal_parameters()</font>"
            <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>\n\t x.size():       </font>" <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> x.<font color='#BB00BB'>size</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>
            <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>\n\t x_lower.size(): </font>" <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> x_lower.<font color='#BB00BB'>size</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>
            <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>\n\t x_upper.size(): </font>" <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> x_upper.<font color='#BB00BB'>size</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>
            <font face='Lucida Console'>)</font>;

        <font color='#009900'>// check the requirements.  Also split the assert up so that the error message isn't huge.
</font>        <font color='#BB00BB'>DLIB_CASSERT</font><font face='Lucida Console'>(</font>max_f_evals <font color='#5555FF'>&gt;</font> <font color='#979000'>1</font> <font color='#5555FF'>&amp;</font><font color='#5555FF'>&amp;</font> eps <font color='#5555FF'>&gt;</font> <font color='#979000'>0</font> <font color='#5555FF'>&amp;</font><font color='#5555FF'>&amp;</font> initial_search_radius <font color='#5555FF'>&gt;</font> eps,
            "<font color='#CC0000'>\t double find_optimal_parameters()</font>"
            <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>\n\t Invalid arguments have been given to this function</font>"
            <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>\n\t initial_search_radius: </font>" <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> initial_search_radius
            <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>\n\t eps:                   </font>" <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> eps
            <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>\n\t max_f_evals:           </font>" <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> max_f_evals
        <font face='Lucida Console'>)</font>;

        <font color='#BB00BB'>DLIB_CASSERT</font><font face='Lucida Console'>(</font> <font color='#BB00BB'>min</font><font face='Lucida Console'>(</font>x_upper <font color='#5555FF'>-</font> x_lower<font face='Lucida Console'>)</font> <font color='#5555FF'>&gt;</font> <font color='#979000'>0</font> <font color='#5555FF'>&amp;</font><font color='#5555FF'>&amp;</font>
                     <font color='#BB00BB'>min</font><font face='Lucida Console'>(</font>x <font color='#5555FF'>-</font> x_lower<font face='Lucida Console'>)</font> <font color='#5555FF'>&gt;</font><font color='#5555FF'>=</font> <font color='#979000'>0</font> <font color='#5555FF'>&amp;</font><font color='#5555FF'>&amp;</font> <font color='#BB00BB'>min</font><font face='Lucida Console'>(</font>x_upper <font color='#5555FF'>-</font> x<font face='Lucida Console'>)</font> <font color='#5555FF'>&gt;</font><font color='#5555FF'>=</font> <font color='#979000'>0</font>,
            "<font color='#CC0000'>\t double find_optimal_parameters()</font>"
            <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>\n\t The bounds constraints have to make sense and also contain the starting point.</font>"
            <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>\n\t min(x_upper - x_lower):                         </font>" <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> <font color='#BB00BB'>min</font><font face='Lucida Console'>(</font>x_upper <font color='#5555FF'>-</font> x_lower<font face='Lucida Console'>)</font> 
            <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>\n\t min(x - x_lower) &gt;= 0 &amp;&amp; min(x_upper - x) &gt;= 0: </font>" <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> <font face='Lucida Console'>(</font><font color='#BB00BB'>min</font><font face='Lucida Console'>(</font>x <font color='#5555FF'>-</font> x_lower<font face='Lucida Console'>)</font> <font color='#5555FF'>&gt;</font><font color='#5555FF'>=</font> <font color='#979000'>0</font> <font color='#5555FF'>&amp;</font><font color='#5555FF'>&amp;</font> <font color='#BB00BB'>min</font><font face='Lucida Console'>(</font>x_upper <font color='#5555FF'>-</font> x<font face='Lucida Console'>)</font> <font color='#5555FF'>&gt;</font><font color='#5555FF'>=</font> <font color='#979000'>0</font><font face='Lucida Console'>)</font>
        <font face='Lucida Console'>)</font>;

        <font color='#009900'>// if the search radius is too big then shrink it so it fits inside the bounds.
</font>        <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>initial_search_radius<font color='#5555FF'>*</font><font color='#979000'>2</font> <font color='#5555FF'>&gt;</font><font color='#5555FF'>=</font> <font color='#BB00BB'>min</font><font face='Lucida Console'>(</font>x_upper<font color='#5555FF'>-</font>x_lower<font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>
            initial_search_radius <font color='#5555FF'>=</font> <font color='#979000'>0.5</font><font color='#5555FF'>*</font><font color='#BB00BB'>min</font><font face='Lucida Console'>(</font>x_upper<font color='#5555FF'>-</font>x_lower<font face='Lucida Console'>)</font><font color='#5555FF'>*</font><font color='#979000'>0.99</font>;


        <font color='#0000FF'><u>double</u></font> objective_val <font color='#5555FF'>=</font> std::numeric_limits<font color='#5555FF'>&lt;</font><font color='#0000FF'><u>double</u></font><font color='#5555FF'>&gt;</font>::<font color='#BB00BB'>infinity</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
        <font color='#0000FF'><u>size_t</u></font> num_iter_used <font color='#5555FF'>=</font> <font color='#979000'>0</font>;
        <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>x.<font color='#BB00BB'>size</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> <font color='#5555FF'>=</font><font color='#5555FF'>=</font> <font color='#979000'>1</font><font face='Lucida Console'>)</font>
        <b>{</b>
            <font color='#009900'>// BOBYQA requires x to have at least 2 variables in it.  So we can't call it in
</font>            <font color='#009900'>// this case.  Instead we call find_min_single_variable().
</font>            matrix<font color='#5555FF'>&lt;</font><font color='#0000FF'><u>double</u></font>,<font color='#979000'>0</font>,<font color='#979000'>1</font><font color='#5555FF'>&gt;</font> <font color='#BB00BB'>temp</font><font face='Lucida Console'>(</font><font color='#979000'>1</font><font face='Lucida Console'>)</font>;
            <font color='#0000FF'>auto</font> ff <font color='#5555FF'>=</font> [<font color='#5555FF'>&amp;</font>]<font face='Lucida Console'>(</font><font color='#0000FF'>const</font> <font color='#0000FF'><u>double</u></font><font color='#5555FF'>&amp;</font> xx<font face='Lucida Console'>)</font>
            <b>{</b>
                temp <font color='#5555FF'>=</font> xx;
                <font color='#0000FF'><u>double</u></font> obj <font color='#5555FF'>=</font> <font color='#BB00BB'>f</font><font face='Lucida Console'>(</font>temp<font face='Lucida Console'>)</font>;  
                <font color='#5555FF'>+</font><font color='#5555FF'>+</font>num_iter_used;
                <font color='#009900'>// keep track of the best x.
</font>                <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>obj <font color='#5555FF'>&lt;</font> objective_val<font face='Lucida Console'>)</font>
                <b>{</b>
                    objective_val <font color='#5555FF'>=</font> obj;
                    x <font color='#5555FF'>=</font> temp;
                <b>}</b>
                <font color='#0000FF'>return</font> obj;
            <b>}</b>;
            <font color='#0000FF'>try</font>
            <b>{</b>
                <font color='#0000FF'><u>double</u></font> dx <font color='#5555FF'>=</font> <font color='#BB00BB'>x</font><font face='Lucida Console'>(</font><font color='#979000'>0</font><font face='Lucida Console'>)</font>;
                <font color='#BB00BB'>find_min_single_variable</font><font face='Lucida Console'>(</font>ff, dx, <font color='#BB00BB'>x_lower</font><font face='Lucida Console'>(</font><font color='#979000'>0</font><font face='Lucida Console'>)</font>, <font color='#BB00BB'>x_upper</font><font face='Lucida Console'>(</font><font color='#979000'>0</font><font face='Lucida Console'>)</font>, eps, max_f_evals, initial_search_radius<font face='Lucida Console'>)</font>;
            <b>}</b> <font color='#0000FF'>catch</font> <font face='Lucida Console'>(</font>optimize_single_variable_failure<font color='#5555FF'>&amp;</font> <font face='Lucida Console'>)</font>
            <b>{</b>
            <b>}</b>
        <b>}</b>
        <font color='#0000FF'>else</font>
        <b>{</b>
            <font color='#0000FF'>auto</font> ff <font color='#5555FF'>=</font> [<font color='#5555FF'>&amp;</font>]<font face='Lucida Console'>(</font><font color='#0000FF'>const</font> matrix<font color='#5555FF'>&lt;</font><font color='#0000FF'><u>double</u></font>,<font color='#979000'>0</font>,<font color='#979000'>1</font><font color='#5555FF'>&gt;</font><font color='#5555FF'>&amp;</font> xx<font face='Lucida Console'>)</font>
            <b>{</b>
                <font color='#0000FF'><u>double</u></font> obj <font color='#5555FF'>=</font> <font color='#BB00BB'>f</font><font face='Lucida Console'>(</font>xx<font face='Lucida Console'>)</font>; 
                <font color='#5555FF'>+</font><font color='#5555FF'>+</font>num_iter_used;
                <font color='#009900'>// keep track of the best x.
</font>                <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>obj <font color='#5555FF'>&lt;</font> objective_val<font face='Lucida Console'>)</font>
                <b>{</b>
                    objective_val <font color='#5555FF'>=</font> obj;
                    x <font color='#5555FF'>=</font> xx;
                <b>}</b>
                <font color='#0000FF'>return</font> obj;
            <b>}</b>;
            <font color='#0000FF'>try</font>
            <b>{</b>
                matrix<font color='#5555FF'>&lt;</font><font color='#0000FF'><u>double</u></font>,<font color='#979000'>0</font>,<font color='#979000'>1</font><font color='#5555FF'>&gt;</font> start_x <font color='#5555FF'>=</font> x;
                <font color='#BB00BB'>find_min_bobyqa</font><font face='Lucida Console'>(</font>ff, start_x, <font color='#979000'>2</font><font color='#5555FF'>*</font>x.<font color='#BB00BB'>size</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font color='#5555FF'>+</font><font color='#979000'>1</font>, x_lower, x_upper, initial_search_radius, eps, max_f_evals<font face='Lucida Console'>)</font>;
            <b>}</b> <font color='#0000FF'>catch</font> <font face='Lucida Console'>(</font>bobyqa_failure<font color='#5555FF'>&amp;</font> <font face='Lucida Console'>)</font>
            <b>{</b>
            <b>}</b>
        <b>}</b>

        <font color='#0000FF'>return</font> objective_val;
    <b>}</b>

<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
<b>}</b>

<font color='#0000FF'>#endif</font> <font color='#009900'>// DLIB_fIND_OPTIMAL_PARAMETERS_Hh_
</font>

</pre></body></html>